//
//  JWMeMemberRechargeVC.m
//  jinwang
//
//  Created by jzb on 2018/4/13.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "JWMeMemberRechargeVC.h"
#import "JWMeMemberRechargeCell.h"
#import "JWMeMenberRechargeModel.h"

@interface JWMeMemberRechargeVC ()
{
    CGFloat VIPHeight;
}
@property (nonatomic, strong) MLMSegmentHead *segHead;
@property (nonatomic, strong) MLMSegmentScroll *segScroll;
@property (nonatomic,strong) JWMeMemberRechargeCell *cell;

@property (nonatomic,strong) NSMutableArray *VIPArr,*VIPImgArr,*VIPBtnArr;
@property (nonatomic,strong) NSMutableArray *SVIPArr,*SVIPImgArr,*SVIPBtnArr;


@end

@implementation JWMeMemberRechargeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"会员充值";
    VIPHeight = 70;
    [self setUpTableView];
    [self makeUrl_user_service];
}
//配置tableView
-(void)setUpTableView{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //注册cell
    
    [self registerCellWithCellClassArray:@[NSStringFromClass([JWMeMemberRechargeCell class])]];
    
}

#pragma mark -UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    self.cell = [tableView dequeueReusableCellWithIdentifier:[JWMeMemberRechargeCell getCellReuseIdentifier]];
    self.cell.contentView.backgroundColor = [UIColor clearColor];
    
    [self.cell.HeaderImg sd_setImageWithURL:[NSURL URLWithString:k_user_headsmall] placeholderImage:[UIImage imageNamed:@"jw_img_404"]];
    
    if (kStringIsEmpty(k_user_phone)) {
        self.cell.PhoneLabel.text = @"账号：";
    }else{
        self.cell.PhoneLabel.text = [NSString stringWithFormat:@"账号：%@",k_user_phone];
    }
    
    if (kStringIsEmpty(k_user_nickname)) {
        self.cell.NicknameLabel.text = @"昵称：";
    }else{
        self.cell.NicknameLabel.text = [NSString stringWithFormat:@"昵称：%@",k_user_nickname];
    }
    
    NSArray *list = @[@"VIP",@"SVIP"];
    _segHead = [[MLMSegmentHead alloc] initWithFrame:CGRectMake(0, 0, self.cell.buyBGView.width, 44) titles:list headStyle:SegmentHeadStyleLine layoutStyle:MLMSegmentLayoutDefault];
    //    _segHead.fontScale = 1.1;
    _segHead.fontSize = 16;
    _segHead.deSelectColor = kRGBColor(80, 80, 80);
    _segHead.selectColor = UIColorHex(0xEEA72B);
    _segHead.lineScale = 1;
    _segHead.bottomLineHeight = .5;
    _segHead.lineColor = UIColorHex(0xEEA72B);
    //    _segHead.headColor = [UIColor clearColor];
    _segHead.bottomLineColor = kRGBColor(230, 230, 230);
    
    _segScroll = [[MLMSegmentScroll alloc] initWithFrame:CGRectMake(0, 44, self.cell.buyBGView.width, VIPHeight*((_VIPArr.count>_SVIPArr.count)?_VIPArr.count:_SVIPArr.count))  vcOrViews:[self vcArr:list.count]];
    
    _segScroll.loadAll = YES;
    _segScroll.showIndex = 0;
    _segScroll.backgroundColor = [UIColor clearColor];
    
    [MLMSegmentManager associateHead:_segHead withScroll:_segScroll completion:^{
        
        [self.cell.buyBGView addSubview:_segHead];
        [self.cell.buyBGView addSubview:_segScroll];
    }];
    self.cell.buyBGViewHeight.constant = VIPHeight*((_VIPArr.count>_SVIPArr.count)?_VIPArr.count:_SVIPArr.count)+44;
    
//    [self.cell.bojianBut addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
//    [self.cell.shopBut addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
//    [self.cell.shopdongtaiBut addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
//    [self.cell.shenbiantianxiaBut addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
//    [self.cell.kuaisushangwuBut addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
//    [self.cell.zhaocheBut addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return self.cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 360+VIPHeight*((_VIPArr.count>_SVIPArr.count)?_VIPArr.count:_SVIPArr.count)+44+10;
}
#pragma mark - 数据源
- (NSArray *)vcArr:(NSInteger)count {
    NSMutableArray *arr = [NSMutableArray array];
    _VIPImgArr = [NSMutableArray array];
    _VIPBtnArr = [NSMutableArray array];
    _SVIPImgArr = [NSMutableArray array];
    _SVIPBtnArr = [NSMutableArray array];
    
    UIView *VIPView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, VIPHeight*_VIPArr.count)];
    
    for (int i = 0; i < _VIPArr.count; i++) {
        JWMeMenberRechargeModel *model = _VIPArr[i];
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, i*VIPHeight, VIPView.width, VIPHeight)];
        view.backgroundColor = [UIColor whiteColor];
        [VIPView addSubview:view];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, view.height-.5, view.width, .5)];
        lineView.backgroundColor = kRGBColor(240, 240, 240);
        [view addSubview:lineView];
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(view.width - 18 - 17, (view.height-18)/2, 18, 18)];
        imageView.image = [UIImage imageNamed:@"jw_grayYuan"];
        imageView.tag = 2000+i;
        [view addSubview:imageView];
        [_VIPImgArr addObject:imageView];
        UILabel *service_priceLel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, view.width-10-18-17-10, 20)];
        NSString *string = [NSString stringWithFormat:@"%@个月%@元",model.service_cycle,model.service_price];
        NSMutableAttributedString *attrDescribeStr = [[NSMutableAttributedString alloc] initWithString:string];
        [attrDescribeStr addAttribute:NSForegroundColorAttributeName value:UIColorHex(0xEEA72B) range:[string rangeOfString:model.service_price]];
        service_priceLel.attributedText = attrDescribeStr;
        service_priceLel.font = [UIFont systemFontOfSize:16];
        [view addSubview:service_priceLel];
        
        UILabel *service_original_priceLel = [[UILabel alloc]initWithFrame:CGRectMake(10, 40, view.width-10-18-17-10, 20)];
        service_original_priceLel.text = [NSString stringWithFormat:@"原价：%@元",model.service_original_price];
        service_original_priceLel.textColor = [UIColor grayColor];
        service_original_priceLel.font = [UIFont systemFontOfSize:14];
        [view addSubview:service_original_priceLel];
        
        UIButton *button = [[UIButton alloc]initWithFrame:view.bounds];
        button.tag = 2100+i;
        [view addSubview:button];
        [button addTarget:self action:@selector(VIPBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        [_VIPBtnArr addObject:button];
    }
    
    
    UIView *SVIPView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, VIPHeight*_SVIPArr.count)];
    
    for (int i = 0; i < _SVIPArr.count; i++) {
        
        JWMeMenberRechargeModel *model = _SVIPArr[i];
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, i*VIPHeight, VIPView.width, VIPHeight)];
        view.backgroundColor = [UIColor whiteColor];
        [SVIPView addSubview:view];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, view.height-.5, view.width, .5)];
        lineView.backgroundColor = kRGBColor(240, 240, 240);
        [view addSubview:lineView];
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(view.width - 18 - 17, (view.height-18)/2, 18, 18)];
        imageView.image = [UIImage imageNamed:@"jw_grayYuan"];
        imageView.tag = 3000+i;
        [view addSubview:imageView];
        [_SVIPImgArr addObject:imageView];
        UILabel *service_priceLel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, view.width-10-18-17-10, 20)];
        NSString *string = [NSString stringWithFormat:@"%@个月%@元",model.service_cycle,model.service_price];
        NSMutableAttributedString *attrDescribeStr = [[NSMutableAttributedString alloc] initWithString:string];
        [attrDescribeStr addAttribute:NSForegroundColorAttributeName value:UIColorHex(0xEEA72B) range:[string rangeOfString:model.service_price]];
        service_priceLel.attributedText = attrDescribeStr;
        service_priceLel.font = [UIFont systemFontOfSize:16];
        [view addSubview:service_priceLel];
        
        UILabel *service_original_priceLel = [[UILabel alloc]initWithFrame:CGRectMake(10, 40, view.width-10-18-17-10, 20)];
        service_original_priceLel.text = [NSString stringWithFormat:@"原价：%@元",model.service_original_price];
        service_original_priceLel.textColor = [UIColor grayColor];
        service_original_priceLel.font = [UIFont systemFontOfSize:14];
        [view addSubview:service_original_priceLel];
        
        UIButton *button = [[UIButton alloc]initWithFrame:view.bounds];
        button.tag = 3100+i;
        [view addSubview:button];
        [button addTarget:self action:@selector(VIPBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        [_SVIPBtnArr addObject:button];
    }
    
    VIPView.backgroundColor = [UIColor clearColor];
    SVIPView.backgroundColor = kRGBColor(245, 245, 245);
    
    [arr addObject:VIPView];
    [arr addObject:SVIPView];
    
    return arr;
}
- (void)VIPBtnOnclick:(UIButton *)sender{
    
    for (UIImageView *imageView  in _VIPImgArr) {
        
        imageView.image = [UIImage imageNamed:@"jw_grayYuan"];
    }
    for (UIImageView *imageView  in _SVIPImgArr) {
        
        imageView.image = [UIImage imageNamed:@"jw_grayYuan"];
    }
    UIImageView *imageView = (UIImageView *)[self.view viewWithTag:sender.tag - 100];
    imageView.image = [UIImage imageNamed:@"jw_sex_btn_sel"];
}
-(void)makeUrl_user_service{
    [[DDNetworkManagerDate makeUrl_user_serviceWithtoken:k_user_token] post_RequestFinshSuccess:^(id responseObject) {
        NSLog(@"----->>%@",responseObject);
        if ([self requestFinshCode:responseObject isShowMessage:NO] == 1) {
           
            NSArray *data = [responseObject objectForKey:kRequsetDataKey];
            if (data.count == 2) {
                 NSMutableArray *arr01 = [NSMutableArray array];
                 NSMutableArray *arr02 = [NSMutableArray array];
                [data[0] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    JWMeMenberRechargeModel *model = [JWMeMenberRechargeModel yy_modelWithDictionary:obj];
                        [arr01 addObject:model];
                }];
                [data[1] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    JWMeMenberRechargeModel *model = [JWMeMenberRechargeModel yy_modelWithDictionary:obj];
                    [arr02 addObject:model];
                }];
                self.VIPArr = arr01.mutableCopy;
                self.SVIPArr = arr02.mutableCopy;
            }
        }
        //刷新
        [self.tableView reloadData];
    } failure:^(id errorObject) {
        [self showBottomMessag:@"网络请求失败"];
    }];
}
-(NSMutableArray *)VIPArr{
    if (!_VIPArr) {
        _VIPArr = [NSMutableArray array];
    }
    return _VIPArr;
}
-(NSMutableArray *)SVIPArr{
    if (!_SVIPArr) {
        _SVIPArr = [NSMutableArray array];
    }
    return _SVIPArr;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
